package com.xunji.standard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunji.standard.common.Constant;
import com.xunji.standard.entity.CaseFile;
import com.xunji.standard.entity.CaseUploadInfo;
import com.xunji.standard.mapper.CaseFileMapper;
import com.xunji.standard.service.CaseFileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xuqiang_imyours
 * @Date: 2025/6/23 15:56
 * @Description:
 **/
@Service
public class CaseFileServiceImpl extends ServiceImpl<CaseFileMapper, CaseFile> implements CaseFileService {

    @Resource
    CaseUploadInfoImpl caseUploadInfoImpl;

    @Override
    public List<CaseFile> listPageFilesByInfos(String fileStatus, ArrayList<Long> infoIds) {
        QueryWrapper<CaseFile> qw = new QueryWrapper<>();
        qw.in("info_id", infoIds).orderByAsc("created_time");
        qw.eq(!StringUtils.isEmpty(fileStatus), "file_status", fileStatus);
        return this.list(qw);
    }

    @Override
    public CaseFile getCaseFileById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public void deleteById(String id) {
        this.baseMapper.deleteById(id);
    }

    @Override
    public void update(CaseFile caseFile) {
        this.baseMapper.updateById(caseFile);
    }

    @Override
    public Page<CaseFile> listPageFilesByInfos(String fileStatus, ArrayList<Long> infoIds, int pageNumber, int pageSize) {
        Page<CaseFile> caseFilePage = new Page<>(pageNumber, pageSize);
        QueryWrapper<CaseFile> qw = new QueryWrapper<>();
        qw.in("info_id", infoIds).orderByAsc("created_time");
        qw.eq(!StringUtils.isEmpty(fileStatus), "file_status", fileStatus);
        return this.baseMapper.selectPage(caseFilePage, qw);
    }

    @Override
    public long getCountByFileStatus(ArrayList<Long> infoIds, String fileStatus) {

        QueryWrapper<CaseFile> qw = new QueryWrapper<>();
        qw.in("info_id", infoIds).orderByAsc("created_time");
        qw.eq(!StringUtils.isEmpty(fileStatus), "file_status", fileStatus);
        return this.baseMapper.selectCount(qw);
    }

    @Override
    public boolean isAllAnalyzed(String caseId) {
        // 拷贝完成，判断是否全部完成解析
        List<CaseUploadInfo> lists = caseUploadInfoImpl.getListByCaseId(caseId);
        ArrayList<Long> infoIds = new ArrayList<>();
        for (CaseUploadInfo info : lists) {
            infoIds.add(info.getId());
        }

        QueryWrapper<CaseFile> qw = new QueryWrapper<>();
        qw.in("info_id", infoIds).eq("file_status", Constant.S_TWO);
        Long l = this.baseMapper.selectCount(qw);
        return l == 0;

    }

    @Override
    public List<CaseFile> selectListByInfoIdAndFileName(String caseId, Long infoId, String fileName) {
        QueryWrapper<CaseFile> qw = new QueryWrapper<>();
        qw.eq("info_id", infoId).like("original_path", fileName);
        return this.baseMapper.selectList(qw);
    }

    @Override
    public void deleteByCaseId(String caseId) {
        List<CaseUploadInfo> list = caseUploadInfoImpl.getListByCaseId(caseId);
        if (list != null && !list.isEmpty()) {
            for (CaseUploadInfo info : list) {
                QueryWrapper<CaseFile> qw = new QueryWrapper<>();
                qw.eq("info_id", info.getId());
                this.baseMapper.delete(qw);
            }
        }
    }

    @Override
    public void updateExcelPassword(String originalPath, CaseFile caseFile, String password) {
        // 变更文件名
        // 变更原始文件名称
        File file = new File(originalPath);
        String name = file.getName();
        String newName = name.substring(0, name.lastIndexOf(".")) + "_[" + password + "]" + name.substring(name.lastIndexOf("."));
        file.renameTo(new File(file.getParent() + File.separator + newName)); // 更名

        caseFile.setOriginalPath(file.getParent() + File.separator + newName);
        caseFile.setFilePassword(password);
        caseFile.setFileStatus(Constant.S_ONE); // 解密成功
        this.update(caseFile);
    }

    @Override
    public boolean isExits(String originalName, List<Long> infoIds) {
        QueryWrapper<CaseFile> qw = new QueryWrapper<>();
        qw.eq("original_name", originalName).in("info_id", infoIds);
        return this.baseMapper.selectCount(qw) > 0;
    }

    @Override
    public void deleteByCaseIdAndInfoId(String caseId, Long infoId) {
        QueryWrapper<CaseFile> qw = new QueryWrapper<>();
        qw.eq("info_id", infoId).eq("case_id", caseId);
        this.baseMapper.delete(qw);
    }

    @Override
    public List<CaseFile> selectListByInfoIdAndCaseId(String caseId, Long infoId) {
        QueryWrapper<CaseFile> qw = new QueryWrapper<>();
        qw.eq("info_id", infoId).eq("case_id", caseId);
        return this.baseMapper.selectList(qw);
    }

    @Override
    public List<CaseFile> selectListByCaseId(String caseId) {
        QueryWrapper<CaseFile> qw = new QueryWrapper<>();
        qw.eq("case_id", caseId);
        return this.baseMapper.selectList(qw);
    }
}
